package com.sikao.app.dao;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.net.BaseCallback;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OkHttpHelper;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.utils.CryptoUtil;
import com.sikao.app.utils.SignatureUtil;
import com.sikao.app.utils.TimeUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by y50 on 2016/4/27.
 */
public abstract class BaseModel {

	public Context context;
	public OnNetBackListener onNetBackListener;
	public Map<String, String> map;
	public String time = TimeUtil.getTime().substring(0, 10);
	public BaseCallback callback;


	protected BaseModel(Context context, OnNetBackListener listener) {
		this.context = context;
		this.onNetBackListener = listener;
		map = new HashMap<>();
	}

	public abstract void request(String data, boolean isShow);
	/**
	 * 取消请求
	 */
	public void cancelRequest() {
		if (callback != null) {
			OkHttpHelper.getInstance().cancelRequest(callback);
		}
	}

	/**
	 * 异步post请求
	 */
	public void post(Map<String, String> map, BaseCallback callback) {
		this.callback = callback;
		OkHttpHelper.getInstance().post(HttpApi.URL, map, callback);
	}

	public void post(String url, BaseCallback callback) {
		this.callback = callback;
		OkHttpHelper.getInstance().post(url, null, callback);
	}

	public void post(String url, Map<String, String> map, BaseCallback callback) {
		this.callback = callback;
		OkHttpHelper.getInstance().post(url, map, callback);
	}
	/**
	 * 异步get请求
	 */
	public void get(String url, BaseCallback callback) {
		this.callback = callback;
		OkHttpHelper.getInstance().get(url, callback);
	}

	public Map<String, String> getMap(String[] array) {
		String signature;
		map.put(HttpApi.API_ARRAY[6], "off");
		if (array.length < 7) {
			signature = SignatureUtil.getSignature(array[4], HttpApi.VER, "off", array[0], array[1], array[2], array[3]);
		} else {
			signature = SignatureUtil.getSignature(array[4], HttpApi.VER, "on", array[0], array[1], array[2], array[3]);
			/*
			加密
		    */
			if (array.length == 7) {
				//无加密密码使用发送时间加密
				array[5] = CryptoUtil.getCode(array[5], CryptoUtil.ENCODE, array[4], 0);
			}
			if (array.length == 8) {
				//有加密密码使用密码加密
				array[5] = CryptoUtil.getCode(array[5], CryptoUtil.ENCODE, array[7], 0);
			}
			if (array.length == 9) {
				//带有有效时间的加密
				array[5] = CryptoUtil.getCode(array[5], CryptoUtil.ENCODE, array[7], Integer.valueOf(array[8]));
			}
		}
		if (array.length < 9) {
			map.put("expiry", "0");
		}
		for (int i = 0; i < array.length; i++) {
			map.put(HttpApi.API_ARRAY[i], array[i]);
		}
		map.put(HttpApi.API_ARRAY[9], signature);
		return map;
	}
	/*			private View noData, noNetwork;//无数据和无网络布局
					//无网络
					if (data == null) {
						//清除数据，数据不为空emptyView不会显示
						if (beans.size() != 0) {
							beans.clear();
							adapter.notifyDataSetChanged();
						}
						//如果无数据布局加载过要隐藏掉
						if (noData != null) {
							noData.setVisibility(View.GONE);
						}
						//判断是否加载过无网络布局，避免重复加载显示
						if (noNetwork == null) {
							noNetwork = getActivity().getLayoutInflater().inflate(R.layout.no_network, null);
							((ViewGroup) refreshlistview.getParent()).addView(noNetwork);
							refreshlistview.setEmptyView(noNetwork);
							Button button = (Button) noNetwork.findViewById(R.id.btn_request);
							button.setOnClickListener(new View.OnClickListener() {
								@Override
								public void onClick(View v) {
									type = 0;
									page = 1;
									getList(true);
								}
							});
						} else {
							noNetwork.setVisibility(View.VISIBLE);
						}
						return;
						}

						//无数据
						if (data.getStatus().equals("0")) {
							//清除数据，数据不为空emptyView不会显示
							if (beans.size() != 0) {
								beans.clear();
								adapter.notifyDataSetChanged();
							}
							//如果无网络布局加载过要隐藏掉
							if (noNetwork != null) {
								noNetwork.setVisibility(View.GONE);
							}
							//判断是否加载过无数据布局，避免重复加载显示
							if (noData == null) {
								noData = getActivity().getLayoutInflater().inflate(R.layout.content_empty, null);
								((ViewGroup) refreshlistview.getParent()).addView(noData);
								refreshlistview.setEmptyView(noData);
								noData.setOnClickListener(new View.OnClickListener() {
									@Override
									public void onClick(View v) {
										type = 0;
										page = 1;
										getList(true);
									}
								});
							} else {
								noData.setVisibility(View.VISIBLE);
							}

						} else {
							Toast.makeText(getActivity(), data.getMsg(), Toast.LENGTH_SHORT).show();
						}*/

}
